package com.wanghlme.leetcodecn;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * <p>剑指 Offer 32 - I. 从上到下打印二叉树</p>
 *
 * @author wanghlme
 * @version 1.0
 */
public class LC_JZ32 {
    public static void main(String[] args) {

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    class Solution {
        public int[] levelOrder(TreeNode root) {
            if (root == null) return new int[0];
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            ArrayList<Integer> tmp = new ArrayList<>();
            while (!queue.isEmpty()) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add(node.right);
            }
            int[] result = new int[tmp.size()];
            for (int i = 0; i < tmp.size(); i++)
                result[i] = tmp.get(i);
            return result;
        }
    }
}
